package cn.xorange.commons.data.utils;

import cn.hutool.core.util.ReflectUtil;
import cn.xorange.commons.data.annotation.MColumn;
import cn.xorange.commons.data.constants.FieldConstants;
import cn.xorange.commons.data.constants.KeyType;
import cn.xorange.commons.data.core.entity.BaseEntity;
import cn.xorange.commons.exception.DataException;
import cn.xorange.commons.utils.lang.ObjectUtils;
import cn.xorange.commons.utils.lang.StringUtils;

import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @author yangjian
 */
public class ParamUtil {
    private ParamUtil(){}

    public static Field getField(Class<?> beanClass, String fieldName){
        Field[] fs = ReflectUtil.getFields(beanClass);
        for(Field field : fs){
            if(field.getName().equals(fieldName)){
                return field;
            }
        }
        throw DataException.entityFieldNameError(fieldName);
    }

    public static Field getKeyField(Class<?> beanClass){
        Field[] fs = ReflectUtil.getFields(beanClass);
        for (Field field:fs){
            MColumn mColumn = field.getAnnotation(MColumn.class);
            if (Objects.nonNull(mColumn) && !KeyType.NOT.equals(mColumn.key())){
                return field;
            }
        }
        return null;
    }

    /**
     * 获取key值 ，无异常输出，无值null
     * @param t 实体
     * @return
     */
    public static <T extends BaseEntity> Object getKeyValue(T t){
        Field field = getKeyField(t.getClass());
        if (Objects.nonNull(field)) {
            return ReflectUtil.getFieldValue(t,field);
        }else {
            return null;
        }
    }

    public static String getColName(Field field,boolean isMapUnderscoreToCamelCase){
        return getColName(field,null,isMapUnderscoreToCamelCase);
    }
    public static String getColName(Field field,MColumn mColumn,boolean isMapUnderscoreToCamelCase){
        if(Objects.isNull(field)){
            throw DataException.dataBandException("");
        }
        if(Objects.isNull(mColumn)){
            mColumn = field.getAnnotation(MColumn.class);
        }
        if(Objects.nonNull(mColumn)){
            if(mColumn.col().isEmpty()){
                if(isMapUnderscoreToCamelCase){
                    return StringUtils.toUnderlineCase(field.getName());
                }else {
                    return field.getName();
                }

            }else {
                return mColumn.col();
            }
        }else {
            return "";
        }
    }


    /** where 值数据前置处理，合并T的paramMap 中的数据 并进行有效格式转化*/
    public static <T extends BaseEntity> Object getWhereFieldVal(T t, Field field, String colName){
        Object objVal = null;
        String fieldName = field.getName();
        if (FieldConstants.CREATE_TIME.equals(fieldName)){
              Object startTime = t.getParams().get("startTime");
              Object endTime = t.getParams().get("endTime");
              if(Objects.nonNull(startTime) && Objects.nonNull(endTime)){
                  Set<Object> set = new HashSet<>();
                  set.add(startTime);
                  set.add(endTime);
                  return set;
              }
        }

        /// 先到 t.param 里去找
        if (t.getParams().containsKey(field.getName())){
            objVal = t.getParams().get(field.getName());
        }else {
            if (t.getParams().containsKey(colName)){
                objVal = t.getParams().get(colName);
            }
        }
        if(Objects.isNull(objVal)){
           return ReflectUtil.getFieldValue(t,field);
        }else {
            // 格式化val
            List<Object> lst = ObjectUtils.toList(objVal);
            if(lst.isEmpty()){
                return DataUtil.formatObj(field.getType().getName(),objVal);
            }else {
                Set<Object> set = new HashSet<>();
                for(Object o : lst){
                    Object v = DataUtil.formatObj(field.getType().getName(),o);
                    set.add(v);
                }
                return set;
            }
        }
    }


}
